package org.gruposp2p.proyectokarma.controller

import grails.converters.JSON
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.Authentication;
import org.gruposp2p.proyectokarma.domain.Peer;
import org.gruposp2p.proyectokarma.domain.Document;
import org.gruposp2p.proyectokarma.util.StringUtils;
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import java.text.SimpleDateFormat

class DocumentController {
	
	def springSecurityService
	def tagService
	def electionOptionService
	
	def save = {
		def userDetails = springSecurityService.principal
		def peer = Peer.get(userDetails.id)
		def document = new Document(peer:peer, name:request.JSON.name, 
			content:request.JSON.content, polling:request.JSON.isPolling, 
			pollingValidTo:new SimpleDateFormat("yyyy-mm-dd").parse(request.JSON.pollingValidTo));
		document.save()
		if (request.JSON.tags) {
			tagService.updateDocumentTags(document, request.JSON.tags)
		}
		if (request.JSON.options) {
			electionOptionService.updateDocumentOptions(document, request.JSON.options)
		}
		response.status = 201 //Following a POST command, this indicates success
		response.setHeader('Location', grailsApplication.config.grails.serverURL + '/documents/' + document.id)
		def result = [id: document.id, peer: document.peer.id,
				dateCreated:document.dateCreated, name:document.name, content:document.content]
		withFormat{
			html{
			  return result
			}
			json {
				render result as JSON
			}
		}
	}
	
	def show = {
		def documentList = []
		def documentsMap = new HashMap()
		if (params.ids?.size() > 0) {
			 Document.getAll(params.ids).collect {document ->
				if (document) documentList << document;
			}
			if (documentList.size() == 0) {
				response.status = 404 //Not Found
				render message(code: 'proyectokarma.document.documentNotFound', args:[params.ids])
				return
			}
		} else {
			documentList =  Document.list(params)
			documentsMap.offset = params.offset
		}
		documentsMap.numberOfDocumentsInSystem = Document.count()
		documentsMap.numberOfDocumentsInRequest = documentList.size()
		documentsMap.documents = documentList.collect {document ->
			return [id: document.id, peer: document.peer.id, peerName: document.peer.name, 
				dateCreated:document.dateCreated, name:document.name, content:document.content,
				tags:document.documentTags?.collect {documentTag -> return documentTag.tag.name}]
		}
		withFormat{
			html{
			  return [documentsMap:documentsMap]
			}
			json {
				render documentsMap as JSON
			}
		}
		
	}
		
	def update = {}
		
	def delete = {
		if (params.id) {
			def document = Document.get(params.id)
			if (document) document.delete()
			else {
				response.status = 404;
				render message(code: 'proyectokarma.document.deleteNotFound', args:[params.id])
				return
			}
			response.status = 200;
			render message(code: 'proyectokarma.document.deleteOK', args:[params.id])
			return
		} else {
			response.status = 400  //Bad Request
			render message(code: 'proyectokarma.document.deleteBadRequest')
			return;
		}
	}
	
}
